/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.server.algorithms;

import java.math.BigInteger;

/**
 *
 * source: algoritmy.net
 */
public class Fibonacci {

    public BigInteger execute(int index) {
        if (index == 0) {
            return BigInteger.ZERO;
        }
        if (index == 1) {
            return BigInteger.ONE;
        }
        BigInteger prePre = BigInteger.ZERO; //predminule cislo
        BigInteger pre = BigInteger.ONE; //minule cislo
        BigInteger result = BigInteger.ZERO; //vysledek
        for (int i = 1; i < index; i++) { //pocitame od druheho indexu
            result = prePre.add(pre); //vysledek je soucet dvou minulych cisel
            prePre = pre; //v dalsim kroku je minule predminulym
            pre = result; //a vysledek je minulym cislem
        }
        return result;
    }
}
